using System;
using System.Data;
using System.Globalization;
using Model;


public partial class ActionTaken : System.Web.UI.Page
{
    private DirectServiceActionTaken _DirectServiceActionTaken = new DirectServiceActionTaken();
    private string whereclause = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            grdListActionTaken.Visible = false;
           
        }
        else 
        {
            whereclause = GetWhereClause();
            txtHiddenWhereClause.Text  = whereclause;
            int numberActionTaken = _DirectServiceActionTaken.GetActionTakenDiplayed(whereclause, null).Rows.Count;
            if (numberActionTaken == 0)
            {
                grdListActionTaken1.Visible = true;
                grdListActionTaken.Visible =false;
                visisbleControl();
            }
            else
            {
                lblTotalActionTaken.Text = "Total Action taken:" + numberActionTaken;
                grdListActionTaken1.Visible = false;
                grdListActionTaken.Visible = true;
                visisbleControl();
            }
        }
 

    }
    private void visisbleControl()
    {
        lblMessage.Visible = false;
        btnNo.Visible = false;
        btnYes.Visible = false;
        lblConfirmation.Visible = false;
        lblActionTaken.Visible = false;
        lblSparepart.Visible = false; 
        
    }
    public int GetPageIndex()
    {
        return Session["pageIndex"] != null
                   ?
                       Int32.Parse(Session["pageIndex"].ToString())
                   : 0;
    }
    public int GetTabIndex()
    {
        int tabIndex = Request.QueryString["tabIndex"] != null
                        ?
                            Int32.Parse(Request.QueryString["tabIndex"])
                        : 0;
        if (Session["defaultTabIndex"] != null)
        {
            tabIndex = Int32.Parse(Session["defaultTabIndex"].ToString());
            Session["defaultTabIndex"] = null;
        }
        return tabIndex;

    }
    protected string GetWhereClause()
    {
        string whereClause = string.Empty;
        string and = "";
        string str = "";

        // Prepare by equipement
        int i = 0;
        if (!string.IsNullOrEmpty(txtWorkOrderID.Text))
        {
            if (int.TryParse(txtWorkOrderID.Text, out i))
            {  
                str = "TASKINWORKORDER.WORKORDERID=" + Int32.Parse(txtWorkOrderID.Text);  
            }
        } 
        if(!string.IsNullOrEmpty(txtStartDate.Text))
        {
            if (!String.IsNullOrEmpty(str))
            {
                str += " AND ";
            }
            DateTime.Parse(txtStartDate.Text, new CultureInfo("en-US", true));
            str += " (reparation.startDate = convert(datetime,'" + txtStartDate.Text + "', 101) )";
           
        
        }
        if(drlEmployeeName.SelectedIndex !=0)
          {
              if (!string.IsNullOrEmpty(str))
              {
                  str += " AND ";
              }
              str += "EMPLOYEENAME=" + "'" + drlEmployeeName.SelectedItem.ToString() + "'";  
          }
        if (!string.IsNullOrEmpty(txtEndDate.Text ))
        {
            if (!String.IsNullOrEmpty(str))
            {
                str += " AND ";
            }
            DateTime.Parse(txtEndDate.Text, new CultureInfo("en-US", true));
            str += " (reparation.endDate = convert(datetime,'" + txtEndDate.Text + "', 101) )";
        }
        if(!string.IsNullOrEmpty(txtStatus.Text))
        {
            if(!string.IsNullOrEmpty(str))
            {
                str += " AND ";
            }
            str += "TASKSTATUS=" + "'" + txtStatus.Text + "'" ;
 
        }
        if(!string.IsNullOrEmpty(str))
        {
            str = "WHERE " + str;
            return str;
        }
        return whereClause;
    }
    protected string GetOrderClause()
    {
        return " Order by Model.Model";
    }
    public void sendMail()
    {
        DataTable tableMail = new DataTable();
        tableMail = _DirectServiceActionTaken.GetSendMail(null ,null);
        if(tableMail.Rows .Count!=0)
        {
            string body = "Forword to employee " + tableMail.Rows[0]["employeename"] + " with your request Workorder number " + tableMail.Rows[0]["workorderid"] + "" + "%0A"
                    + "We has to reparation on " + tableMail.Rows[0]["startdate"] + " and finish on " + tableMail.Rows[0]["enddate"] + " %0A"
                    + "By status " + tableMail.Rows[0]["TASKSTATUS"] + "";
            Response.Write("data='mailto:" + tableMail.Rows[0]["EMPLOYEEEMAILADRESS"] + "?subject=workorderid " + tableMail.Rows[0]["workorderid"] + "&body=" + body + "'" + ";");
        
        }
    }
    public void SendingMail(object objectsending)
    {  
   //   string str = Request.QueryString["data"].ToString();
	    sendMail();
     
	              
    }
    protected void bntFilter_Click(object sender, EventArgs e)
    {

        whereclause = GetWhereClause();
        //ValueUseOtherPage.queryActionTakenToListActionTanken = GetWhereClause();
       // Session["defaultTabIndex"] = 1;
    }
    public int GetActionTakenId()
    {
        return !String.IsNullOrEmpty(Request.QueryString["actionTakenId"]) ? Int32.Parse(Request.QueryString["actionTakenId"]) : 0;
    }
    protected void bntValidate_Click(object sender, EventArgs e)
    {

    }
}




